package com.ray.friendslist;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ray.friendslist.adaptor.UserProfileCursorAdapter;
import com.ray.friendslist.content.contract.FriendsContract;
import com.ray.friendslist.database.DBHelperFactory;
import com.ray.friendslist.images.ImageLoader;
import com.ray.friendslist.service.VKFriendsService;
import com.ray.friendslist.service.VKUserProfileService;
import com.ray.friendslist.service.common.CommonService;

public class VkUserProfileActivity extends FragmentActivity implements
		LoaderCallbacks<Cursor>, OnItemClickListener {

	private static final String ITEM_ID = "ITEM_ID";

	private static final String FROM = "FROM";

	private String USER_ID = "USER_ID";

	private ListView mListView;

	private ProgressBar mProgressBar;

	private UserProfileCursorAdapter mCursorAdapter;

	private BroadcastReceiver mBroadcastReceiver;

	private View mViewProfileInfoHeader;

	private TextView mHeaderUserNameTextView;

	private ImageView mHeaderUserPhotoImageView;

	private Cursor mCursor;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_user_profile);
		mProgressBar = (ProgressBar) findViewById(R.id.user_profile_activity_progress_bar);
		mListView = (ListView) findViewById(R.id.wall_items_list);
		mViewProfileInfoHeader = getLayoutInflater().inflate(
				R.layout.header_view_profile, null);
		bindHeaderViews();
		mBroadcastReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				Log.d("onReceive", "");

				String action = intent.getAction();
				if (action.equals(VKFriendsService.ACTION_ON_ERROR)) {
					mProgressBar.setVisibility(View.GONE);
					Toast.makeText(
							VkUserProfileActivity.this,
							intent.getStringExtra(CommonService.EXTRA_KEY_MESSAGE),
							Toast.LENGTH_LONG).show();
				} else if (action.equals(CommonService.ACTION_ON_SUCCESS)) {
					mProgressBar.setVisibility(View.GONE);
				}
			}
		};
		IntentFilter filter = new IntentFilter();
		filter.addAction(CommonService.ACTION_ON_ERROR);
		filter.addAction(CommonService.ACTION_ON_SUCCESS);
		registerReceiver(mBroadcastReceiver, filter);
		mCursorAdapter = new UserProfileCursorAdapter(this, null);
		mListView.addHeaderView(mViewProfileInfoHeader);
		mListView.setOnItemClickListener(this);
		mListView.setAdapter(mCursorAdapter);
		getSupportLoaderManager().initLoader(0, null, this);
	}

	private void bindHeaderViews() {
		mCursor = DBHelperFactory
				.getInstance()
				.getHelper(FriendsContract.FriendsColumns.TABLE_NAME)
				.getItems(null,
						FriendsContract.FriendsColumns.VK_FRIEND_ID + " = ?",
						new String[] { getIntent().getStringExtra(USER_ID) });
		if (mCursor != null) {
			mHeaderUserNameTextView = (TextView) mViewProfileInfoHeader
					.findViewById(R.id.view_profile_activity_header_text_view);
			mHeaderUserPhotoImageView = (ImageView) mViewProfileInfoHeader
					.findViewById(R.id.view_profile_activity_header_image_view);
			if (mCursor.moveToFirst()) {
				ImageLoader
						.getInstance()
						.bind(mHeaderUserPhotoImageView,
								mCursor.getString(mCursor
										.getColumnIndex(FriendsContract.FriendsColumns.VK_FRIEND_PHOTO_BIG_URL)));
				mHeaderUserNameTextView
						.setText(mCursor.getString(mCursor
								.getColumnIndex(FriendsContract.FriendsColumns.VK_FRIEND_NAME)));
			}
		}
		mCursor.close();
	}

	public void onRefreshButtonClick(View view) {
		mProgressBar = (ProgressBar) findViewById(R.id.user_profile_activity_progress_bar);
		mProgressBar.setVisibility(ProgressBar.VISIBLE);
		DBHelperFactory.getInstance().deleteTable(
				FriendsContract.UserInfoColumns.TABLE_NAME,
				FriendsContract.UserInfoColumns.USER_ID + " = ?",
				new String[] { getIntent().getStringExtra(USER_ID) });
		Intent intent = new Intent(this, VKUserProfileService.class);
		intent.putExtra(USER_ID, getIntent().getStringExtra(USER_ID));
		startService(intent);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		Log.d("VkUserProfileActivity",""+cursor.getCount());
		if (cursor.getCount() == 0) {
			mProgressBar = (ProgressBar) findViewById(R.id.user_profile_activity_progress_bar);
			mProgressBar.setVisibility(ProgressBar.VISIBLE);
			DBHelperFactory.getInstance().deleteTable(
					FriendsContract.UserInfoColumns.TABLE_NAME,
					FriendsContract.UserInfoColumns.USER_ID + " = ?",
					new String[] { getIntent().getStringExtra(USER_ID) });
			Intent intent=new Intent(this, VKUserProfileService.class);
			intent.putExtra(USER_ID, getIntent().getStringExtra(USER_ID));
			startService(intent);
		}
		mCursorAdapter.swapCursor(cursor);
	}

	public void onLoaderReset(Loader<Cursor> arg0) {
		mCursorAdapter.swapCursor(null);
	}

	public Loader<Cursor> onCreateLoader(int paramInt, Bundle bundle) {
		String where = FriendsContract.UserInfoColumns.USER_ID;
		String[] selectionArgs = new String[] { getIntent().getStringExtra(
				USER_ID) };
		return new CursorLoader(this,
				FriendsContract.UserInfoColumns.CONTENT_URI, null, where
						+ " = ?", selectionArgs, null);
	}

	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long arg3) {
		Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
		Intent intent = new Intent(this, VKFeedViewActivity.class);
		Log.d("WALL ID",
				"WALL ID "
						+ cursor.getString(cursor
								.getColumnIndex(FriendsContract.UserInfoColumns.ITEM_ID)));
		intent.putExtra(ITEM_ID, cursor.getString(cursor
				.getColumnIndex(FriendsContract.UserInfoColumns.ITEM_ID)));
		intent.putExtra(FROM, "WALL");
		startActivity(intent);
	}
}
